package com.heima.file.config;

import com.heima.file.service.FileStorageService;
import com.heima.file.service.impl.MinIOFileStorageService;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties(MinIOConfigProperties.class)
public class MinIOAutoConfiguration {
    @Autowired
    private MinIOConfigProperties minioConfigProperties;

    @Bean
    @ConditionalOnMissingBean
    public MinioClient buildMinioClient(){
        return MinioClient
                .builder()
                .credentials(minioConfigProperties.getAccessKey(), minioConfigProperties.getSecretKey())
                .endpoint(minioConfigProperties.getEndpoint())
                .build();
    }
    @Bean
    @ConditionalOnMissingBean
    public FileStorageService fileStorageService(MinioClient minioClient){
        return new MinIOFileStorageService(minioClient, minioConfigProperties);
    }
}
